<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5">
        <pointList v-on:handlePointClick="handlePointClick"></pointList>
      </el-col>

      <el-col :span="19">

        <el-table v-loading="loading" :data="pointParamList" :max-height="tabHt" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="参数" align="center" prop="paramInfo.name" />
          <el-table-column label="参数ID" align="center" prop="dpParamId" />
<!--          <el-table-column label="是否报警" align="center" prop="isAlarm" :formatter="isAlarmFormat"/>-->
          <el-table-column label="从站地址" align="center" prop="slaveId" />
          <el-table-column label="功能码" align="center" prop="funCode" />
          <el-table-column label="寄存器地址" align="center" prop="dataAddr" />
          <el-table-column label="地址位" align="center" prop="dataBit" />
          <el-table-column label="存储区" align="center" prop="storageArea" />
          <el-table-column label="存储号" align="center" prop="storageNo" />
          <el-table-column label="数值类型" align="center" prop="dataType" />
          <el-table-column label="解析顺序" align="center" prop="decode" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--<pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />-->

        <!-- 添加或修改点位参数关联对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-row>
<!--              <el-col :span="12">-->
<!--                <el-form-item label="是否报警" prop="isAlarm">-->
<!--                  <el-select v-model="form.isAlarm" placeholder="请选择是否报警">-->
<!--                    <el-option-->
<!--                      v-for="dict in sysIsOptions"-->
<!--                      :key="dict.dictValue"-->
<!--                      :label="dict.dictLabel"-->
<!--                      :value="dict.dictValue"-->
<!--                    ></el-option>-->
<!--                  </el-select>-->
<!--                </el-form-item>-->
<!--              </el-col>-->
              <el-col :span="12">
                <el-form-item label="参数ID" prop="dpParamId">
                  <el-input v-model="form.dpParamId" placeholder="请输入参数ID" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="参数类型" prop="dpParamType">
                  <el-select v-model="form.dpParamType" placeholder="请选择参数类型">
                    <el-option
                      v-for="dict in dpParamTypeOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="参数类别" prop="dpParamClass">
                  <el-select v-model="form.dpParamClass" placeholder="请选择参数类别">
                    <el-option
                      v-for="dict in dpParamClassOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="从站地址" prop="slaveId">
                  <el-input v-model="form.slaveId" placeholder="请输入从站地址" oninput ="value=value.replace(/[^0-9]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="功能码" prop="funCode">
                  <el-input v-model="form.funCode" placeholder="请输入功能码" oninput ="value=value.replace(/[^0-9]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="寄存器" prop="dataAddr">
                  <el-input v-model="form.dataAddr" placeholder="请输入寄存器地址" oninput ="value=value.replace(/[^0-9]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="地址位" prop="dataBit">
                  <el-input v-model="form.dataBit" placeholder="请输入地址位" oninput ="value=value.replace(/[^0-9-]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="存储区" prop="storageArea">
                  <el-input v-model="form.storageArea" placeholder="请输入存储区" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="存储号" prop="storageNo">
                  <el-input v-model="form.storageNo" placeholder="请输入存储号" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="数值类型" prop="dataType">
                  <el-input v-model="form.dataType" placeholder="请输入数值类型" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="解析顺序" prop="decode">
                  <el-input v-model="form.decode" placeholder="请输入解析顺序" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="换算系数" prop="dpConversion">
                  <el-input v-model="form.dpConversion" placeholder="请输入换算系数" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="关系式" prop="dpRelation">
                  <el-input v-model="form.dpRelation" placeholder="请输入关系式" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="小数位" prop="median">
                  <el-input v-model="form.median" placeholder="请输入小数位" oninput ="value=value.replace(/[^0-9]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="上量程" prop="upRange">
                  <el-input v-model="form.upRange" placeholder="请输入上量程" oninput ="value=value.replace(/[^0-9.]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="下量程" prop="lowRange">
                  <el-input v-model="form.lowRange" placeholder="请输入下量程" oninput ="value=value.replace(/[^0-9.]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="报警上限" prop="upValue">
                  <el-input v-model="form.upValue" placeholder="请输入报警上限" oninput ="value=value.replace(/[^0-9.]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="报警下限" prop="lowValue">
                  <el-input v-model="form.lowValue" placeholder="请输入报警下限" oninput ="value=value.replace(/[^0-9.]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="均告警数" prop="avgNum">
                  <el-input v-model="form.avgNum" placeholder="请输入平均数" oninput ="value=value.replace(/[^0-9]/g,'')" style="width: 180px"/> 组
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="方差限值" prop="avgVal">
                  <el-input v-model="form.avgVal" placeholder="请输入方差限值" oninput ="value=value.replace(/[^0-9.]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="均值上限" prop="upAvg">
                  <el-input v-model="form.upAvg" placeholder="请输入均值上限" oninput ="value=value.replace(/[^0-9.]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="均值下限" prop="lowAvg">
                  <el-input v-model="form.lowAvg" placeholder="请输入均值下限" oninput ="value=value.replace(/[^0-9.]/g,'')"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="连续相同" prop="continuityNum">
                  <el-input v-model="form.continuityNum" placeholder="请输入连续相同" oninput ="value=value.replace(/[^0-9]/g,'')" style="width: 180px"/> 组
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否工艺" prop="isAlarm">
                  <el-select v-model="form.isgy" placeholder="请选择是否工艺">
                    <el-option
                      v-for="dict in sysIsOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否详细" prop="isAlarm">
                  <el-select v-model="form.isxx" placeholder="请选择是否详细">
                    <el-option
                      v-for="dict in sysIsOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>

      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listPointParam, getPointParam, delPointParam, addPointParam, updatePointParam, exportPointParam } from "@/api/basic/pointParam";
import pointList from "@/views/components/pointTreeList";

export default {
  name: "PointAlarmConf",
  components: {
    pointList
  },
  data() {
    return {
      //  是否
      sysIsOptions: [{dictLabel:'否',dictValue:'N'},{dictLabel:'是',dictValue:'Y'}],
      dpParamTypeOptions: [{dictLabel:'只读',dictValue:'R'},{dictLabel:'只写',dictValue:'W'},{dictLabel:'读写',dictValue:'RW'},{dictLabel:'未知',dictValue:'UN'}],
      dpParamClassOptions: [{dictLabel:'数值',dictValue:'D'},{dictLabel:'状态',dictValue:'S'},{dictLabel:'告警',dictValue:'A'},{dictLabel:'配置',dictValue:'C'}],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 点位参数关联表格数据
      pointParamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100000,
        pointId: null,
        paramId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  computed: {
    tabHt(){
      return this.$store.state.app.tableHeight.ht + 60
    }
  },
  created() {

  },
  methods: {
    isAlarmFormat(row, column) {
      return this.selectDictLabel(this.sysIsOptions, row.isAlarm);
    },
    handlePointClick(obj){
      this.queryParams.pointId=obj.pointId;
      this.getList();
    },
    /** 查询点位参数关联列表 */
    getList() {
      this.loading = true;
      listPointParam(this.queryParams).then(response => {
        this.pointParamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pointId: null,
        paramId: null,
        brand: null,
        model: null,
        isAlarm: null,
        lowValue: null,
        upValue: null,
        avgNum: null,
        avgVal: null,
        paramCodeId: null,
        dpParamId: null,
        slaveId: null,
        funCode: null,
        dataAddr: null,
        dataBit: null,
        storageArea: null,
        storageNo: null,
        dataType: null,
        decode: null,
        dpConversion: null,
        dpRelation: null,
        median: null,
        upRange: null,
        lowRange: null,
        upAvg: null,
        lowAvg: null,
        continuityNum: null,
        dpParamType: null,
        dpParamClass: null,
        isgy: 'N',
        gyx:null,
        gyy:null,
        isxx: 'Y',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加点位参数关联";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPointParam(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改点位参数关联";
      });
    },
    /**
     *判断是否是数字
     *
     **/
    isRealNum(val){
      // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除，
      if(val === "" || val == null){
        return true;
      }
      if(!isNaN(val)){
        //对于空数组和只有一个数值成员的数组或全是数字组成的字符串，isNaN返回false，例如：'123'、[]、[2]、['123'],isNaN返回false,
        //所以如果不需要val包含这些特殊情况，则这个判断改写为if(!isNaN(val) && typeof val === 'number' )
        return true;
      } else{
        return false;
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {

            if(!this.isRealNum(this.form.lowValue) || !this.isRealNum(this.form.upValue) || !this.isRealNum(this.form.upRange) || !this.isRealNum(this.form.lowRange)
              || !this.isRealNum(this.form.avgVal) || !this.isRealNum(this.form.upAvg) || !this.isRealNum(this.form.lowAvg)){
              this.msgError("有错误值，请修改！");
              return;
            }
            updatePointParam(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除点位参数关联编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPointParam(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有点位参数关联数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPointParam(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
